import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}

const routes = [
    {
        path: '/',
        name: 'home',
        meta: {name: '非遗展示中心'},
        component: () => import('@/views/homeView.vue'),
        redirect: '/CultureCommunity',
    },
    {
        path: '/ProjectDetail',
        name: 'ProjectDetail',
        meta: {name: '项目详情门户'},
        component: () => import('@/views/ProjectDetailView.vue')
    },
    {
        path: '/CultureCommunity',
        name: 'CultureCommunity',
        meta: {name: '文化社区'},
        component: () => import('@/views/CultureCommunityView.vue'),
        redirect: '/CultureInfo',
        children: [
            {
                path: '/CultureInfo',
                name: 'CultureInfo',
                meta: {name: '文化社区详细文化信息'},
                component: () => import('@/views/CultureCommunity/CultureInfoView.vue'),
            },
            {
                path: '/CultureInfoDetail',
                name: 'CultureInfoDetail',
                meta: {name: '详细文化信息'},
                component: () => import('@/views/CultureCommunity/CultureInfoDetailView.vue'),
            }
        ]
    },
    {
        path: '/CultureMap',
        name: 'CultureMap',
        meta: {name: '文化地图导航'},
        component: () => import('@/views/CultureMapView.vue')
    },
    {
        path: '/CultureDeclare',
        name: 'CultureDeclare',
        meta: {name: '非遗申报'},
        component: () => import('@/views/CultureDeclareView.vue')
    },
    {
        path: '/Shop',
        name: 'Shop',
        meta: {name: '周边商城'},
        component: () => import('@/views/ShopView.vue')
    },
    {
        path: '/ShopDetail',
        name: 'ShopDetail',
        meta: {name: '店铺详细'},
        component: () => import('@/views/shop/ShopDetail.vue')
    },
    {
        path: '/ProductDetail',
        name: 'ProductDetail',
        meta: {name: '产品详细'},
        component: () => import('@/views/shop/ProductDetail.vue')
    },
    {
        path: '/MyCollect',
        name: 'MyCollect',
        meta: {name: '我的收藏'},
        component: () => import('@/views/shop/MyCollect.vue')
    },
    {
        path: '/ShoppingCart',
        name: 'ShoppingCart',
        meta: {name: '购物车'},
        component: () => import('@/views/shop/ShoppingCart.vue')
    },
    {
        path: '/OrderCenter',
        name: 'OrderCenter',
        meta: {name: '订单中兴'},
        component: () => import('@/views/shop/OrderCenter.vue')
    },
    {
        path: '/PersonCenterView',
        name: 'PersonCenterView',
        meta: {name: '个人中心'},
        component: () => import('@/views/PersonCenterView.vue'),
        redirect: '/CenterView',
        children: [
            {
                path: '/CenterView',
                name: 'CenterView',
                meta: {name: '个人资料'},
                component: () => import('@/views/person/CenterView.vue'),
            },
            {
                path: '/UpdatePassword',
                name: 'UpdatePassword',
                meta: {name: '修改密码'},
                component: () => import('@/views/person/UpdatePassword.vue'),
            },
            {
                path: '/PayView',
                name: 'PayView',
                meta: {name: '充值'},
                component: () => import('@/views/person/PayView'),
            }
        ]
    },
    {
        path: '/AiTool',
        name: 'AiTool',
        meta: {name: 'AI工具'},
        component: () => import('@/views/AiToolView.vue'),
        redirect: '/ChatView',
        children: [
            {
                path: '/ChatView',
                name: 'ChatView',
                meta: {name: '聊天机器人'},
                component: () => import('@/views/AiTool/ChatView.vue'),
            },
            {
                path: '/PaintingView',
                name: 'PaintingView',
                meta: {name: '绘画机器人'},
                component: () => import('@/views/AiTool/PaintingView.vue'),
            }
        ]
    },
    {
        path: '/Login',
        name: 'Login',
        meta: {name: '登录'},
        component: () => import('@/views/LoginView.vue')
    },
    {
        path: '/Register',
        name: 'Register',
        meta: {name: '注册'},
        component: () => import('@/views/RegisterView.vue')
    },
    {
        path: '/*',
        component: () => import('@/views/404View.vue')
    }
]

const router = new VueRouter({
    routes,
    mode: 'history',
})

// 假设有一个方法 isLoggedIn 用于检查用户是否已登录
// function isLoggedIn() {
//     // 这里可以是检查 localStorage、sessionStorage 或其他存储方式
//     return !!localStorage.getItem('token');
// }

// 添加导航守卫
// router.beforeEach((to, from, next) => {
//     const requiresAuth = to.matched.some(record => record.meta.requiresAuth);
//     if (requiresAuth && !isLoggedIn()) {
//         next({name: 'Login'});
//     } else {
//         next();
//     }
// });

export default router
